package org.itboys.admin.service.lasted;

import org.apache.commons.lang.StringUtils;
import org.itboys.admin.entity.lasted.SystemLog;
import org.itboys.admin.entity.lasted.SystemUser;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017年3月17日
 * 描述：供应链金融__日志__Service
 *
 */
@Service
public class SystemLogService extends BaseMongoService<SystemLog> {

    @Resource(name = "adminDS")
    private MongoDataSource ds;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return ds;
    }
    @Autowired
    private  SystemUserService userService;
    @Override
    protected Class<SystemLog> getEntityClass() {
        return SystemLog.class;
    }

    public PageResult<SystemLog> selectLog(HttpServletRequest request) throws ParseException {
        Map<String,Object> param =new HashMap<String, Object>();
        param.put("isDeleted", 0);
        List<SystemUser>users=userService.list(param);
        Date firstDate=null;
        Date lastDate=null;
        String first=request.getParameter("firstDate");
        String last=request.getParameter("lastDate");
        String userName=request.getParameter("userName");
        if(StringUtils.isNotBlank(userName)){
            for(SystemUser user:users){
                if(userName.equals(user.getName())){
                    param.put("creator",user.getId());
                    break;
                }else {
                    param.put("creator",0);
                }
            }
        }
        if(StringUtils.isNotBlank(first)){
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            firstDate=sdf.parse(first);
            param.put("ct >",firstDate.getTime());
    }
        if(StringUtils.isNotBlank(last)){
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            lastDate=sdf.parse(last);
            param.put("ct <",lastDate.getTime());
        }

        Map<String,String> containsparam = ParamUtil.packageMongoVagueParam(request,"userName");
        PageResult<SystemLog> result=super.containpageQuery(request,containsparam,param);
        for(SystemLog log:result.getData()){
            for(SystemUser user:users){
                if(LongUtil.isNotZreo(log.getCreator())&&log.getCreator().longValue()==user.getId()){
                        log.setCreateName(user.getName());
                        break;
                }
            }
        }
        return result;
    }

    /**
     * 新增日志
     * @param title。标题
     * @param remark。备注。可以填入操作的参数
     */
    public void saveLog(String title,String remark){
        super.save(new SystemLog(title,remark));
    }
}
